# pragma once
# include <stdint.h>
# include <atomic>

struct AVCodecContext;
struct AVCodec;
struct AVPacket;
struct AVFrame;
struct SwsContext;
struct AVFormatContext;
struct AVStream;
struct SwrContext;
enum AVSampleFormat;

class EncoderFilter;

# define SENDFRAMESIZE 4096


class _declspec(dllexport) AudioEncoder {
public:
	AudioEncoder(AVSampleFormat _input_format, int _sample_rate, int _bit_rate);

	uint8_t* getEncodeCodecCtx(size_t* _size) const;
	void addFilter(EncoderFilter* _filter);

	~AudioEncoder();

	void onPcmData(uint8_t* _data, size_t _sample_count, size_t _sample_size);

private:
	EncoderFilter* m_filter = nullptr;

	AVCodecContext* m_audio_encode_codec_ctx = nullptr;
	AVCodec* m_audio_encode_codec = nullptr;
	AVFrame* m_audio_encode_frame = nullptr;
	AVPacket* m_audio_encode_packet = nullptr;
	uint8_t* m_audio_frame_buf = nullptr;
	uint8_t* m_audio_encode_buffer = nullptr;
	size_t m_audio_encode_buffer_size = 0;

	SwrContext* m_audio_swr_ctx = nullptr;
	uint8_t* m_audio_swr_buffer = nullptr;

	std::atomic_bool m_ready_encode = false;
};
